package com.objetdirect.gwt.umldrawer.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormHandler;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormSubmitEvent;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

@SuppressWarnings("deprecation")
public class TutorialsPageContent implements PageContent {

	private VerticalPanel mainPanel = new VerticalPanel();
	private VerticalPanel tutorialContent = new VerticalPanel();
	private HorizontalPanel tutorialWithImage = new HorizontalPanel();
	private VerticalPanel tutorialPanel = new VerticalPanel();
	private Image umlImage = new Image();
	private Label tutorialsLabel = new Label() ;
	private Button submit = new Button();
	private Image header = new Image();
	private Image footer = new Image();
	
	@Override
	public void setPageContent() {
		header.setUrl("images/header.png");
		header.setWidth("1201px");
		header.setStyleName("headerImage");
		
		tutorialsLabel.setText("Tutorials");
		tutorialsLabel.setStyleName("pagesHeader");
		
		final FormPanel form = new FormPanel();
		form.setAction(GWT.getModuleBaseURL() + "upload");
		
		//Set the form with the POST method 
		form.setEncoding(FormPanel.ENCODING_MULTIPART);
		form.setMethod(FormPanel.METHOD_POST);
		
		final FileUpload fileUpload = new FileUpload();
		fileUpload.setName("uploadFormatElement");
		
		submit.setText("Submit");
		submit.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				form.submit();
			}
		});
		
		form.addFormHandler(new FormHandler() {
			@Override
			public void onSubmitComplete(FormSubmitCompleteEvent event) { //It fires second
				Window.alert(event.getResults());
			}
			
			@Override
			public void onSubmit(FormSubmitEvent event) { // It fires first
				// TODO Auto-generated method stub
				//Window.alert(form.toString());
				//event.setCancelled(true); this cancels the form.
				Window.alert(fileUpload.getFilename().toString());
			}
		});
		
		footer.setUrl("images/footer.png");
		footer.setWidth("1200px");
		
		tutorialContent.add(new HeaderButtons().getHeaderButtons());
		tutorialContent.add(tutorialsLabel);
		tutorialContent.add(fileUpload);
		tutorialContent.add(submit);
		
		umlImage.setUrl("images/image.jpg");
		umlImage.setStyleName("homeLeftImage");
		
		tutorialWithImage.add(umlImage);
		tutorialWithImage.add(tutorialContent);
		
		tutorialPanel.add(tutorialWithImage);
		tutorialPanel.add(new PrivacyBar().getprivacyBar());
		tutorialPanel.setStyleName("border");
		
		mainPanel.add(header);
		mainPanel.add(tutorialPanel);
		mainPanel.add(footer);
	}

	@Override
	public VerticalPanel getPageContent() {
		setPageContent();
		return mainPanel;
	}

}
